var shares = null, bhref = false;
var Intent = null, File = null, Uri = null, main = null;
var share_msg=null,share_link=null;
/**
 * 更新分享服务
 */
function updateSerivces() {
    plus.share.getServices(function (s) {
        shares = {};
        for (var i in s) {
            var t = s[i];
            shares[t.id] = t;
        }
    }, function (e) {
        //outSet("获取分享服务列表失败：" + e.message);
    });
}
/**
 * 调用系统分享
 */
function shareSystem($member_id,msg,link) {
    share_msg=msg;
    share_link=link;
    if($member_id<=0){
        pclicked('index.php?act=member_index');return false;
    }
    updateSerivces();
    if (plus.os.name == "Android") {
        Intent = plus.android.importClass("android.content.Intent");
        File = plus.android.importClass("java.io.File");
        Uri = plus.android.importClass("android.net.Uri");
        main = plus.android.runtimeMainActivity();
    }
    if (true||plus.os.name !== "Android") {//ios没有系统分享，调用自定义分享
        var ids=[{id:"qq"},{id:"weixin",ex:"WXSceneSession"},{id:"weixin",ex:"WXSceneTimeline"},{id:"sinaweibo"}],
            bts=[{title:"分享到QQ"},{title:"发送给微信好友"},{title:"分享到微信朋友圈"},{title:"分享到新浪微博"}];
        plus.nativeUI.actionSheet({cancel:"取消",buttons:bts},
            function(e){
                var i=e.index;
                if(i>0){
                    if(i==1)bhref=true;//QQ分享标识
                    shareAction(ids[i-1].id,ids[i-1].ex);
                }
            }
        );
        return;
    }
    var intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "");//d1mcw
    intent.putExtra(Intent.EXTRA_TEXT, share_msg+share_link);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    main.startActivity(Intent.createChooser(intent, "系统分享"));
}
/**
 * 分享操作
 * @param {String} id
 */
function shareAction(id, ex) {
    var s = null;
    if (!id || !(s = shares[id])) {
        //console.log("无效的分享服务！");
        return;
    }
    if (s.authenticated) {
        //console.log("---已授权---");
        shareMessage(s, ex);
    } else {
        //console.log("---未授权---");
        s.authorize(function () {
            shareMessage(s, ex);
        }, function (e) {
            //console.log("认证授权失败：" + e.code + " - " + e.message);
        });
    }
}
/**
 * 发送分享消息
 * @param {plus.share.ShareService} s
 */
function shareMessage(s, ex) {
    var msg = {content: share_msg, extra: {scene: ex}};
    if(bhref){//此处QQ分享才需要
        msg.href=share_link;
        msg.title='给你介绍个新的进货渠道：美财网';
        msg.thumbs=["_www/logo/01.png"];
        msg.pictures=["_www/logo/01.png"];
    }
    //console.log(JSON.stringify(msg));
    s.send(msg, function(){
        alert('分享到"' + s.description + '"成功！');
    }, function (e) {
        alert('分享失败，请重新分享');
        //alert('分享到"' + s.description + '"失败: ' + e.code + " - " + e.message);
    });
}



